#--------------------------------------------------------------------------------------------------
#Script for compile Yatak Operation System
#Author : Ilya Churaev
#Started : 4 May 2012	Updated : 22 Feb 2013
#File Name : makefile   Purpose : compile Yatak
#--------------------------------------------------------------------------------------------------
-include conf/arch.mk

# FLAGS
ifeq "$(MAKEFLAGS)" "DEBUG"
	END_DIR = debug
	CFLAGS = -fomit-frame-pointer -g -m32 -I$(INC_DIR) -Wfatal-errors -nostdlib -nostdinc
	CPPFLAGS = -Wall -g -fno-tree-scev-cprop -m32 -O1 -fno-rtti -fno-default-inline -fomit-frame-pointer -falign-functions=16 -foptimize-sibling-calls -ffreestanding -fno-stack-protector --no-exceptions -I $(INC_DIR)
	NASMFLAGS = -O99 -g -f elf32
	ASFLAG = -g
	ifeq "$(ARCH)" "x86"
		LDFLAGS = -melf_i386 -g
	else
		LDFLAGS = -g
	endif
else
	END_DIR = release
	CFLAGS = -fomit-frame-pointer -m32 -I$(INC_DIR) -Wfatal-errors -nostdlib -nostdinc
	CPPFLAGS = -Wall -O1 -fno-rtti -m32 -fno-tree-scev-cprop -fno-default-inline -fomit-frame-pointer -falign-functions=16 -foptimize-sibling-calls -ffreestanding -fno-stack-protector --no-exceptions -I $(INC_DIR)
	NASMFLAGS = -O99 -f elf32
	ASFLAG =
	ifeq "$(ARCH)" "x86"
		LDFLAGS = -melf_i386
	else
		LDFLAGS =
	endif
endif

ifeq "$(ARCH)" "x86"
	LDARGS = $(OS_OBJS) -L$(LIB_DIR) -lmc -lc
else
	LDARGS = $(OS_OBJS) -L$(LIB_DIR) 
endif

ifdef GCCPREFIX
SETTINGGCCPREFIX := true
else
-include conf/gcc.mk
endif
-include conf/rules.mk

#LINK SCRIPT
LD_SCRIPT = $(OS_DIR)/link-$(ARCH).ld

#INCLUDE FILES
INC_DIR = $(OS_DIR)/include

# DIRECTORIES
OS_DIR = src
KERNEL_DIR = $(OS_DIR)/kernel
LIB_DIR = $(OS_DIR)/lib
LIBC_DIR = $(OS_DIR)/libc
INITRD_DIR = disk_initrd

KERNEL_DIRS := $(ARCH_DIR) $(ARCH_DIR)/task task $(ARCH_DIR)/pm $(ARCH_DIR)/interrupt $(ARCH_DIR)/cpu $(ARCH_DIR)/mm gui guard log x86emu termio fs fs/initrd .
OS_DIRS := time cmos shell shell/apps drivers/block/ide drivers/vga/bochs drivers/network/realtek drivers/network/3c509 rsa user net net/ip net/socket
KERNEL_DIRS := $(addprefix $(KERNEL_DIR)/,$(KERNEL_DIRS))
OS_DIRS := $(addprefix $(OS_DIR)/,$(OS_DIRS))
SOURCE_DIRS := $(KERNEL_DIRS) $(OS_DIRS)

# BINARIES
LIB_BIN = $(LIB_DIR)/libmc.a
LIBC_BIN = $(LIBC_DIR)/libc.a
KERNEL_BIN = $(END_DIR)/kernel.bin
INITRD_DISK = $(END_DIR)/initrd.img
STINITRD_DISK = $(INITRD_DIR)/initrd.img

#=================================KERNEL OBJECTS=====================================================
search_files := $(addsuffix /*.asm,$(SOURCE_DIRS))
OS_OBJS := $(patsubst %.asm,%.o,$(wildcard $(search_files)))
search_files := $(addsuffix /*.S,$(SOURCE_DIRS))
OS_OBJS += $(patsubst %.S,%.o,$(wildcard $(search_files)))
search_files := $(addsuffix /*.c,$(SOURCE_DIRS))
OS_OBJS += $(patsubst %.c,%.o,$(wildcard $(search_files)))
search_files := $(addsuffix /*.cpp,$(SOURCE_DIRS))
OS_OBJS += $(patsubst %.cpp,%.o,$(wildcard $(search_files)))

.PHONY: clean

#RULES
all: $(OS_OBJS)
	@test -f $(KERNEL_BIN) || rm -f $(KERNEL_BIN)
	@test -d $(END_DIR) || mkdir $(END_DIR)
	@make -C $(LIB_DIR) MAKEFLAGS=$(MAKEFLAGS)
	#@test -f $(LIBC_BIN) || make -C $(LIBC_DIR)
	@echo $(OS_OBJS)
	@$(LD) -T $(LD_SCRIPT) $(LDFLAGS) -o $(KERNEL_BIN) $(LDARGS)
	@make -C $(INITRD_DIR)
	
%.o : %.asm
	@echo "======================================================================="
	@echo $@ $<
	@echo "======================================================================="
	@nasm $(NASMFLAGS) -o $@ $<
	@echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
	
%.o : %.c
	@echo "======================================================================="
	@echo $@ $<
	@echo "======================================================================="
	@$(CC) $(CFLAGS) -c -o $@ $<
	@echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
	
%.o : %.cpp
	@echo "======================================================================="
	@echo $@ $<
	@echo "======================================================================="
	@$(CXX) $(CPPFLAGS) -c -o $@ $<
	@echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
	
%.o: %.S
	@echo "======================================================================="
	@echo $@ $<
	@echo "======================================================================="
	$(AS) $(ASFLAG) -c -o $@ $<
	@echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
	
clean:
	@make -C $(LIB_DIR) clean
	@make -C $(LIBC_DIR) clean
	@make -C $(INITRD_DIR) clean
	@rm -f $(OS_OBJS)
	@rm -f $(INITRD_DISK)
